/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvEvaluationvehicule;
import fr.cggveritas.crewintranet.jms.entities.CvTypeevaluationvehicule;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvTypeevaluationvehiculeJpaController implements Serializable {

    public CvTypeevaluationvehiculeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvTypeevaluationvehicule cvTypeevaluationvehicule) throws PreexistingEntityException, Exception {
        if (cvTypeevaluationvehicule.getCvEvaluationvehiculeList() == null) {
            cvTypeevaluationvehicule.setCvEvaluationvehiculeList(new ArrayList<CvEvaluationvehicule>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvEvaluationvehicule> attachedCvEvaluationvehiculeList = new ArrayList<CvEvaluationvehicule>();
            for (CvEvaluationvehicule cvEvaluationvehiculeListCvEvaluationvehiculeToAttach : cvTypeevaluationvehicule.getCvEvaluationvehiculeList()) {
                cvEvaluationvehiculeListCvEvaluationvehiculeToAttach = em.getReference(cvEvaluationvehiculeListCvEvaluationvehiculeToAttach.getClass(), cvEvaluationvehiculeListCvEvaluationvehiculeToAttach.getIdevaluationvehicule());
                attachedCvEvaluationvehiculeList.add(cvEvaluationvehiculeListCvEvaluationvehiculeToAttach);
            }
            cvTypeevaluationvehicule.setCvEvaluationvehiculeList(attachedCvEvaluationvehiculeList);
            em.persist(cvTypeevaluationvehicule);
            for (CvEvaluationvehicule cvEvaluationvehiculeListCvEvaluationvehicule : cvTypeevaluationvehicule.getCvEvaluationvehiculeList()) {
                CvTypeevaluationvehicule oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListCvEvaluationvehicule = cvEvaluationvehiculeListCvEvaluationvehicule.getCvTypeevaluationvehiculeIdtypeevaluation();
                cvEvaluationvehiculeListCvEvaluationvehicule.setCvTypeevaluationvehiculeIdtypeevaluation(cvTypeevaluationvehicule);
                cvEvaluationvehiculeListCvEvaluationvehicule = em.merge(cvEvaluationvehiculeListCvEvaluationvehicule);
                if (oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListCvEvaluationvehicule != null) {
                    oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListCvEvaluationvehicule.getCvEvaluationvehiculeList().remove(cvEvaluationvehiculeListCvEvaluationvehicule);
                    oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListCvEvaluationvehicule = em.merge(oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListCvEvaluationvehicule);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvTypeevaluationvehicule(cvTypeevaluationvehicule.getIdtypeevaluation()) != null) {
                throw new PreexistingEntityException("CvTypeevaluationvehicule " + cvTypeevaluationvehicule + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvTypeevaluationvehicule cvTypeevaluationvehicule) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeevaluationvehicule persistentCvTypeevaluationvehicule = em.find(CvTypeevaluationvehicule.class, cvTypeevaluationvehicule.getIdtypeevaluation());
            List<CvEvaluationvehicule> cvEvaluationvehiculeListOld = persistentCvTypeevaluationvehicule.getCvEvaluationvehiculeList();
            List<CvEvaluationvehicule> cvEvaluationvehiculeListNew = cvTypeevaluationvehicule.getCvEvaluationvehiculeList();
            List<String> illegalOrphanMessages = null;
            for (CvEvaluationvehicule cvEvaluationvehiculeListOldCvEvaluationvehicule : cvEvaluationvehiculeListOld) {
                if (!cvEvaluationvehiculeListNew.contains(cvEvaluationvehiculeListOldCvEvaluationvehicule)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvEvaluationvehicule " + cvEvaluationvehiculeListOldCvEvaluationvehicule + " since its cvTypeevaluationvehiculeIdtypeevaluation field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CvEvaluationvehicule> attachedCvEvaluationvehiculeListNew = new ArrayList<CvEvaluationvehicule>();
            for (CvEvaluationvehicule cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach : cvEvaluationvehiculeListNew) {
                cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach = em.getReference(cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach.getClass(), cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach.getIdevaluationvehicule());
                attachedCvEvaluationvehiculeListNew.add(cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach);
            }
            cvEvaluationvehiculeListNew = attachedCvEvaluationvehiculeListNew;
            cvTypeevaluationvehicule.setCvEvaluationvehiculeList(cvEvaluationvehiculeListNew);
            cvTypeevaluationvehicule = em.merge(cvTypeevaluationvehicule);
            for (CvEvaluationvehicule cvEvaluationvehiculeListNewCvEvaluationvehicule : cvEvaluationvehiculeListNew) {
                if (!cvEvaluationvehiculeListOld.contains(cvEvaluationvehiculeListNewCvEvaluationvehicule)) {
                    CvTypeevaluationvehicule oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListNewCvEvaluationvehicule = cvEvaluationvehiculeListNewCvEvaluationvehicule.getCvTypeevaluationvehiculeIdtypeevaluation();
                    cvEvaluationvehiculeListNewCvEvaluationvehicule.setCvTypeevaluationvehiculeIdtypeevaluation(cvTypeevaluationvehicule);
                    cvEvaluationvehiculeListNewCvEvaluationvehicule = em.merge(cvEvaluationvehiculeListNewCvEvaluationvehicule);
                    if (oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListNewCvEvaluationvehicule != null && !oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListNewCvEvaluationvehicule.equals(cvTypeevaluationvehicule)) {
                        oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListNewCvEvaluationvehicule.getCvEvaluationvehiculeList().remove(cvEvaluationvehiculeListNewCvEvaluationvehicule);
                        oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListNewCvEvaluationvehicule = em.merge(oldCvTypeevaluationvehiculeIdtypeevaluationOfCvEvaluationvehiculeListNewCvEvaluationvehicule);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvTypeevaluationvehicule.getIdtypeevaluation();
                if (findCvTypeevaluationvehicule(id) == null) {
                    throw new NonexistentEntityException("The cvTypeevaluationvehicule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeevaluationvehicule cvTypeevaluationvehicule;
            try {
                cvTypeevaluationvehicule = em.getReference(CvTypeevaluationvehicule.class, id);
                cvTypeevaluationvehicule.getIdtypeevaluation();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvTypeevaluationvehicule with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvEvaluationvehicule> cvEvaluationvehiculeListOrphanCheck = cvTypeevaluationvehicule.getCvEvaluationvehiculeList();
            for (CvEvaluationvehicule cvEvaluationvehiculeListOrphanCheckCvEvaluationvehicule : cvEvaluationvehiculeListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvTypeevaluationvehicule (" + cvTypeevaluationvehicule + ") cannot be destroyed since the CvEvaluationvehicule " + cvEvaluationvehiculeListOrphanCheckCvEvaluationvehicule + " in its cvEvaluationvehiculeList field has a non-nullable cvTypeevaluationvehiculeIdtypeevaluation field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cvTypeevaluationvehicule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvTypeevaluationvehicule> findCvTypeevaluationvehiculeEntities() {
        return findCvTypeevaluationvehiculeEntities(true, -1, -1);
    }

    public List<CvTypeevaluationvehicule> findCvTypeevaluationvehiculeEntities(int maxResults, int firstResult) {
        return findCvTypeevaluationvehiculeEntities(false, maxResults, firstResult);
    }

    private List<CvTypeevaluationvehicule> findCvTypeevaluationvehiculeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvTypeevaluationvehicule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvTypeevaluationvehicule findCvTypeevaluationvehicule(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvTypeevaluationvehicule.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvTypeevaluationvehiculeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvTypeevaluationvehicule> rt = cq.from(CvTypeevaluationvehicule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
